package net.zhengxinyun.performance.service.department;

import net.zhengxinyun.performance.entity.DepartmentEntity;
import net.zhengxinyun.performance.filter.DepartmentFilter;
import net.zhengxinyun.performance.mapper.Department.DepartmentEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * Created by hyp1202 on 2019/1/17.
 */
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    private DepartmentEntityMapper departmentEntityMapper;

    @Autowired
    public DepartmentServiceImpl(DepartmentEntityMapper departmentEntityMapper) {
        this.departmentEntityMapper = departmentEntityMapper;
    }

    @Override
    public String insert(DepartmentEntity departmentEntity) {
        DepartmentFilter departmentFilter = new DepartmentFilter();
        departmentFilter.setDepartment(departmentEntity.getDepartment());
        List<DepartmentFilter> list = departmentEntityMapper.query(departmentFilter);
        if (list.size()!=0){
            return "已有该部门";
        }
        int i = departmentEntityMapper.insertSelective(departmentEntity);
        if (i == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "添加失败，请重试";
        }
        return "添加成功";
    }

    @Override
    public String update(DepartmentEntity departmentEntity) {
        int i = departmentEntityMapper.updateByPrimaryKeySelective(departmentEntity);
        if (i == 0){
            return "修改失败，请重试";
        }
        return "修改成功";
    }

    @Override
    public String delete(DepartmentEntity departmentEntity) {
        int i = departmentEntityMapper.deleteByPrimaryKey(departmentEntity.getId());
        if (i == 0){
            return "删除失败，请重试";
        }
        return "删除成功";
    }

    @Override
    public List<DepartmentFilter> query(DepartmentFilter departmentFilter) {
        List<DepartmentFilter> list = departmentEntityMapper.query(departmentFilter);
        return list;
    }

    @Override
    public Integer queryForCount(DepartmentFilter departmentFilter) {
        return departmentEntityMapper.queryForCount(departmentFilter);
    }

    @Override
    public List<String> selectForDepartment() {
        return departmentEntityMapper.selectForDepartment();
    }
}
